package com.kgc.warehouse.bussness.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kgc.warehouse.bussness.entity.Customer;
import com.kgc.warehouse.bussness.entity.Goods;
import com.kgc.warehouse.bussness.entity.Sales;
import com.kgc.warehouse.bussness.entity.Salesback;
import com.kgc.warehouse.bussness.service.CustomerService;
import com.kgc.warehouse.bussness.service.GoodsService;
import com.kgc.warehouse.bussness.service.SalesbackService;
import com.kgc.warehouse.bussness.vo.SalesBackVO;
import com.kgc.warehouse.system.common.CodeMsg;
import com.kgc.warehouse.system.common.ResponseResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("salesback")
public class SaleBackController {

    @Resource
    private SalesbackService salesbackService;

    @Resource
    private CustomerService customerService;
    @Resource
    private GoodsService goodsService;

    @RequestMapping("addSalesback")
    public ResponseResult addSalesback(Integer id, Integer number, String remark, HttpSession session){
        try {
            salesbackService.addSalesback(id,number,remark,session);
            return new ResponseResult(CodeMsg.BACKINPORT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CodeMsg.BACKINPORT_FAIL);
        }
    }

    @RequestMapping("loadAllSalesback")
    public ResponseResult loadAllSalesback(SalesBackVO salesBackVO){
        Page<Salesback> salesbackPage = new Page<>(salesBackVO.getPage(), salesBackVO.getLimit());
        LambdaQueryWrapper<Salesback> salesbackLambdaQueryWrapper = new LambdaQueryWrapper<>();
        salesbackLambdaQueryWrapper.eq(salesBackVO.getCustomerid()!=null&&salesBackVO.getCustomerid()!=0,Salesback::getCustomerid,salesBackVO.getCustomerid());
        salesbackLambdaQueryWrapper.eq(salesBackVO.getGoodsid()!=null&&salesBackVO.getGoodsid()!=0,Salesback::getGoodsid,salesBackVO.getGoodsid());
        salesbackLambdaQueryWrapper.ge(salesBackVO.getStartTime()!=null,Salesback::getSalesbacktime,salesBackVO.getStartTime());
        salesbackLambdaQueryWrapper.le(salesBackVO.getEndTime()!=null,Salesback::getSalesbacktime,salesBackVO.getEndTime());
        salesbackLambdaQueryWrapper.orderByDesc(Salesback::getSalesbacktime);


        Page<Salesback> page = salesbackService.page(salesbackPage, salesbackLambdaQueryWrapper);
        List<Salesback> records = page.getRecords();
        long total = page.getTotal();

        List<Salesback> collect = records.stream().map(record -> {
            Customer customer = customerService.getById(record.getCustomerid());
            if (ObjectUtils.isNotNull(customer)) {
                record.setCustomername(customer.getCustomername());
            }
            Goods goods = goodsService.getById(record.getGoodsid());
            if (ObjectUtils.isNotNull(goods)) {
                record.setGoodsname(goods.getGoodsname());
                record.setSize(goods.getSize());
            }
            return record;
        }).collect(Collectors.toList());
        return new ResponseResult(collect,total,CodeMsg.SUCCESS);
    }


    @RequestMapping("deleteSalesback")
    public ResponseResult deleteSalesback(Integer id){
        try {
            salesbackService.deleteSalesback(id);
            return new ResponseResult(CodeMsg.DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CodeMsg.DELETE_FAIL);
        }

    }
}
